﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageProcessing.Utilities;
using ImageProcessing.IntermediateClasses;

namespace ImageProcessing.DataEntryDialogs
{
    public partial class CustomConvolution : Form
    {
        double[,] matrixValues;
        CustomMatrix values;

        public CustomConvolution(ref CustomMatrix matrix)
        {
            InitializeComponent();
            this.matrixValues = new double[3, 3];
            this.CreateDataGridMatrix(3);
            values = matrix;
        }

        private void CreateDataGridMatrix(int size)
        {
            try
            {
                DataTable table = new DataTable();
                for (int i = 0; i < size; i++)
                {
                    table.Columns.Add((i + 1).ToString());
                    table.Rows.Add(table.NewRow());
                }
                this.dataGridMatrix.DataSource = table;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw;
            }
        }

        private void numericUpDownMask_ValueChanged(object sender, EventArgs e)
        {
            this.CreateDataGridMatrix(int.Parse(this.numericUpDownMask.Value.ToString()));
            matrixValues = new double[int.Parse(this.numericUpDownMask.Value.ToString()), int.Parse(this.numericUpDownMask.Value.ToString())];
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < (int)this.numericUpDownMask.Value; i++)
                {
                    for (int j = 0; j < (int)this.numericUpDownMask.Value; j++)
                    {
                        matrixValues[i, j] = float.Parse(this.dataGridMatrix.Rows[i].Cells[j].Value.ToString());
                    }
                }
                if (rdCutOff.Checked == true)
                {
                    values.Type = NeighbourhoodOperations.PostProcessing.cutOff;
                }
                else
                    if (rdNormalization.Checked == true)
                    {
                        values.Type = NeighbourhoodOperations.PostProcessing.normalization;
                    }
                    else
                    {
                        values.Type = NeighbourhoodOperations.PostProcessing.none;
                    }
                values.Matrix = this.matrixValues;
                values.MatrixSize = (int)this.numericUpDownMask.Value;
                this.Close();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                MessageBox.Show("Error saving matrix information.");
            }
        }
    }
}
